//% color="#AA278D" iconWidth=50 iconHeight=40
namespace oled12864 {
  //% block="OLED 128*64 initialize I2C address [ADDR]" blockType="command"
  //% ADDR.shadow="number"   ADDR.defl="0x3c"
  export function initOled(param: any) {
    const addr = param.ADDR.code;

    Generator.addInclude('U8g2lib.h', '#include <U8g2lib.h>');
    Generator.addInclude('DFRobot_NeoPixel', '#include <DFRobot_NeoPixel.h>');

    Generator.addObject(`neoPixel_33`, `DFRobot_NeoPixel`, `neoPixel_33;`);

    Generator.addInclude(`oled12864Begin`, `// 函数声明\n` +
      `void oled12864Begin(uint8_t addr, uint8_t dir);\n` +
      `void oled12864Print(String text, uint8_t x, uint8_t y);\n` +
      `// 创建对象\n` +
      `U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2_12864(U8G2_R0, U8X8_PIN_NONE);\n`
    );

    Generator.addInclude(`static functions 1`, `// Static functions\n` +
      `void oled12864Begin(uint8_t addr, uint8_t dir) {\n` +
      ` u8g2_12864.setI2CAddress(addr<<1);\n` +
      ` u8g2_12864.begin();\n` +
      ` u8g2_12864.enableUTF8Print();\n` +
      ` u8g2_12864.setFont(u8g2_font_wqy16_t_gb2312);\n` +
      ` u8g2_12864.setFontDirection(dir);\n` +
      ` u8g2_12864.firstPage();\n` +
      `}\n` +
      `void oled12864Print(String text, uint8_t x, uint8_t y) {\n` +
      ` u8g2_12864.setCursor(x, y);\n` +
      ` u8g2_12864.print(text);\n` +
      ` u8g2_12864.nextPage();\n` +
      `}`
    );

    Generator.addSetup(`oled12864Begin`, `oled12864Begin(${addr}, 0);\n` +
      `  neoPixel_33.begin(33, 1);\n` +
      `  neoPixel_33.setBrightness(20);\n` +
      `  neoPixel_33.setRangeColor(${0}, ${0}, 0xFFFFFF);\n`
    );
  }

  //% block="OLED 128*64 display text [TEXT] in coordinates X:[XV] Y:[YV]" blockType="command"
  //% TEXT.shadow="string"   TEXT.defl="Mind+育伟达"
  //% XV.shadow="range"   XV.params.min=0    XV.params.max=127    XV.defl=8
  //% YV.shadow="range"   YV.params.min=0    YV.params.max=47    YV.defl=16
  export function displayText(param: any) {
    const text = param.TEXT.code;
    const xv = param.XV.code;
    const yv = param.YV.code;
    Generator.addCode(`oled12864Print(${text}, ${xv}, ${yv + 16});`)
  }

  //% block="OLED 128*64 clear screen" blockType="command"
  export function clearScreen() {
    Generator.addCode(`u8g2_12864.clear();`)
  }

  //% block="Key function module" blockType="command"
  export function keyModule() {
    Generator.addInclude(`valInit`, `// 动态变量\n` +
      `volatile float mind_n_ch;\n` +
      `volatile float mind_n_en;\n`
    );

    Generator.addSetup(`valchInit`, `mind_n_ch = 0; // 左右按键`);
    Generator.addSetup(`valenInit`, `mind_n_en = 1; // 确认按键`);
    Generator.addSetup(`taskFree`, `taskFree();`);

    const leftBtn = 19
      , rightBtn = 18
      , confirmBtn = 5
      , maxLength = 3
      , xc = 50
      , yc = 40

    Generator.addCode(`if ((digitalRead(${leftBtn}) == 0) && (mind_n_ch > 0)) {\n` +
      `   delay(10);\n` +
      `   if ((digitalRead(${leftBtn}) == 0) && (mind_n_ch > 0)) {\n` +
      `       mind_n_ch -= 1;\n` +
      `   }\n` +
      `   while (digitalRead(${leftBtn}) == 0) {\n` +
      `       delay(10);\n` +
      `   }\n` +
      `   u8g2_12864.clear();\n` +
      ` }\n` +
      `\n` +
      ` if ((digitalRead(${rightBtn}) == 0) && (mind_n_ch < ${maxLength})) {\n` +
      `   delay(10);\n` +
      `   if ((digitalRead(${rightBtn}) == 0) && (mind_n_ch < ${maxLength})) {\n` +
      `       mind_n_ch += 1;\n` +
      `   }\n` +
      `   while (digitalRead(${rightBtn}) == 0) {\n` +
      `       delay(10);\n` +
      `   }\n` +
      `   u8g2_12864.clear();\n` +
      ` }\n` +
      `\n` +
      ` if ((digitalRead(${confirmBtn}) == 0)) {\n` +
      `   delay(10);\n` +
      `   if ((digitalRead(${confirmBtn}) == 0)) {\n` +
      `       mind_n_en = !mind_n_en;\n` +
      `   }\n` +
      `   while (digitalRead(${confirmBtn}) == 0) {\n` +
      `       delay(10);\n` +
      `   }\n` +
      ` }\n` +
      `\n` +
      ` while (mind_n_en == 0) {\n` +
      `   delay(10);\n` +
      `   if ((mind_n_en==0)) {\n` +
      `     if ((mind_n_ch==0)) {\n` +
      `       taskStart(loop1);\n` +
      `     }\n` +
      `     if ((mind_n_ch==1)) {\n` +
      `       taskStart(loop2);\n` +
      `     }\n` +
      `     if ((mind_n_ch==2)) {\n` +
      `       taskStart(loop3);\n` +
      `     }\n` +
      `     if ((mind_n_ch==3)) {\n` +
      `       taskStart(loop4);\n` +
      `     }\n` +
      `     if ((mind_n_ch==4)) {\n` +
      `       taskStart(loop5);\n` +
      `     }\n` +
      `     if ((mind_n_ch==5)) {\n` +
      `       taskStart(loop6);\n` +
      `     }\n` +
      `     if ((mind_n_ch==6)) {\n` +
      `       taskStart(loop7);\n` +
      `     }\n` +
      `     if ((mind_n_ch==7)) {\n` +
      `       taskStart(loop8);\n` +
      `     }\n` +
      `   }\n` +
      `   if ((digitalRead(${confirmBtn}) == 0)) {\n` +
      `       delay(10);\n` +
      `       if ((digitalRead(${confirmBtn}) == 0)){\n` +
      `         mind_n_en = !mind_n_en;\n` +
      `       }\n` +
      `       while (digitalRead(${confirmBtn}) == 0) {\n` +
      `         delay(10);\n` +
      `       }\n` +
      `   }\n` +
      ` }\n` +
      `\n` +
      ` if ((mind_n_ch==0)) {\n` +
      `   oled12864Print("00", ${xc}, ${yc});\n` +
      ` }\n` +
      `\n` +
      ` if ((mind_n_ch==1)) {\n` +
      `   oled12864Print("11", ${xc}, ${yc});\n` +
      ` }\n` +
      `\n` +
      ` if ((mind_n_ch==2)) {\n` +
      `   oled12864Print("22", ${xc}, ${yc});\n` +
      ` }\n` +
      `\n` +
      ` if ((mind_n_ch==3)) {\n` +
      `   oled12864Print("33", ${xc}, ${yc});\n` +
      ` }\n` +
      `\n` +
      ` if ((mind_n_en==1)) {\n` +
      `   if ((mind_n_ch==0)) {\n` +
      `     taskFree(loop1);\n` +
      `   }\n` +
      `   if ((mind_n_ch==1)) {\n` +
      `     taskFree(loop2);\n` +
      `   }\n` +
      `   if ((mind_n_ch==2)) {\n` +
      `     taskFree(loop3);\n` +
      `   }\n` +
      `   if ((mind_n_ch==3)) {\n` +
      `     taskFree(loop4);\n` +
      `   }\n` +
      `   if ((mind_n_ch==4)) {\n` +
      `     taskFree(loop5);\n` +
      `   }\n` +
      `   if ((mind_n_ch==5)) {\n` +
      `     taskFree(loop6);\n` +
      `   }\n` +
      `   if ((mind_n_ch==6)) {\n` +
      `     taskFree(loop7);\n` +
      `   }\n` +
      `   if ((mind_n_ch==7)) {\n` +
      `     taskFree(loop8);\n` +
      `   }\n` +
      ` }`
    )
  }
}
